using System.Collections.Generic;

namespace QGameFramework.DateNode
{
    public class DataNodeComponent : BaseComponent
    {
        /// <summary>
        /// 数据字典
        /// 数据以封包保存
        /// </summary>
         Dictionary<string, object> DataNode;

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="reset"></param>
        public void TrySetData(string key, object value, bool reset = true)
        {
            if (!DataNode.ContainsKey(key))
            {
                DataNode[key] = value;
                return;
            }
            else if (DataNode.ContainsKey(key) && reset)
            {
                DataNode[key] = value;
            }
        }

        public void TryGetValue<T>(string key, out T value) where T : new()
        {
            value = DataNode.ContainsKey(key) ? (T)DataNode[key] : new T();
        }

        public override void OnInit()
        {
            base.OnInit();
            DataNode = new Dictionary<string, object>();
            DataNode.Clear();
        }
    }
}